package com.corpgovernment.organization.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BudgetAlarmUtil {

    /**
     * 是否预算告警
     *
     * @param surplus 预算剩余
     * @param total   预算总金额
     * @param alarm   告警阀值
     * @return
     */
    public static boolean isAlarm(BigDecimal surplus, BigDecimal total, BigDecimal alarm) {
        return surplus.compareTo(total.multiply(alarm.divide(new BigDecimal(100), 4, RoundingMode.DOWN))) <= 0;
    }

    /**
     * 是否预算告警
     *
     * @param total     预算总金额
     * @param used      实际使用
     * @param occupancy occupancy
     * @param alarm     告警阀值
     * @return
     */
    public static boolean isAlarm(BigDecimal total, BigDecimal used, BigDecimal occupancy, BigDecimal alarm) {
        return isAlarm(calSurplus(total, used, occupancy), total, alarm);
    }

    /**
     * 是否超预算
     *
     * @param fee
     * @param surplus
     * @return
     */
    public static boolean isOverBudget(BigDecimal fee, BigDecimal surplus) {
        return fee.compareTo(surplus) > 0;
    }

    /**
     * 计算剩余预算
     *
     * @param total     预算总金额
     * @param used      实际使用
     * @param occupancy 在途预占
     * @return
     */
    public static BigDecimal calSurplus(BigDecimal total, BigDecimal used, BigDecimal occupancy) {
        return total.subtract(used.add(occupancy));
    }

    /**
     * 计算预算总额
     *
     * @param init
     * @param addTotal
     * @return
     */
    public static BigDecimal calTotal(BigDecimal init, BigDecimal addTotal) {
        return init.add(addTotal);
    }
}